package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.CouponInfoBean;
import cc.rengu.igas.channel.upqc.facade.bean.PayeeInfoBean;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.B2CQueryCouponRequest;
import cc.rengu.igas.channel.upqc.facade.response.B2CQueryCouponResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import java.util.List;

/**
 * 付款方营销查询
 * reqType:0180000903
 */
public class B2cQueryCouponService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        B2CQueryCouponRequest b2CQueryCouponRequest = new B2CQueryCouponRequest();
        ConvertUtil.convertOutput(b2CQueryCouponRequest);
        return b2CQueryCouponRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        B2CQueryCouponResponse b2CQueryCouponResponse = new B2CQueryCouponResponse();
        B2CQueryCouponRequest b2CQueryCouponRequest = (B2CQueryCouponRequest) request;
        ConvertUtil.convertInput(b2CQueryCouponRequest);
        packagePayerInfo(b2CQueryCouponRequest.getPayerInfo());
        convertRiskInfo(b2CQueryCouponRequest.getRiskInfo());
        packagePayeeInfo(b2CQueryCouponRequest.getPayeeInfoBean());

        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, b2CQueryCouponRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, b2CQueryCouponRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(b2CQueryCouponResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(b2CQueryCouponResponse.getRespCode())) {
            if (xmlTreeUtil.isExist(UpqcTreeNodeConstant.couponInfoStr)) {
                String couponInfoStr = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.couponInfoStr);
                List<CouponInfoBean> couponInfoBeanList = JSON.parseArray(new String(Base64.decodeBase64(couponInfoStr)), CouponInfoBean.class);
                for (CouponInfoBean couponInfoBean : couponInfoBeanList) {
                    if (couponInfoBean.getOffstAmt() != null) {
                        couponInfoBean.setOffstAmt(AmountUtil.changeF2Y(couponInfoBean.getOffstAmt()));
                    }
                }
                b2CQueryCouponResponse.setCouponInfoList(couponInfoBeanList);
            }
            b2CQueryCouponResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            b2CQueryCouponResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(b2CQueryCouponResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        B2CQueryCouponResponse b2CQueryCouponResponse = (B2CQueryCouponResponse) response.getResult();
        ConvertUtil.convertInput(b2CQueryCouponResponse);
    }

    private void packagePayerInfo(PayerInfo payerInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (payerInfo != null && isNotEmpty(payerInfo)) {
            StringBuffer payerInfoBuff = new StringBuffer(UpqcParamConstant.LEFT_BRACE);
            if (null != payerInfo.getAccNo() && !payerInfo.getAccNo().isEmpty()) {
                payerInfoBuff.append("accNo" + UpqcParamConstant.EQUAL + payerInfo.getAccNo() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getName() && !payerInfo.getName().isEmpty()) {
                payerInfoBuff.append("name" + UpqcParamConstant.EQUAL + payerInfo.getName() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getPayerBankInfo() && !payerInfo.getPayerBankInfo().isEmpty()) {
                payerInfoBuff.append("payerBankInfo" + UpqcParamConstant.EQUAL + payerInfo.getPayerBankInfo() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getIssCode() && !payerInfo.getIssCode().isEmpty()) {
                payerInfoBuff.append("issCode" + UpqcParamConstant.EQUAL + payerInfo.getIssCode() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCertifTp() && !payerInfo.getCertifTp().isEmpty()) {
                payerInfoBuff.append("certifTp" + UpqcParamConstant.EQUAL + payerInfo.getCertifTp() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCertifId() && !payerInfo.getCertifId().isEmpty()) {
                payerInfoBuff.append("certifId" + UpqcParamConstant.EQUAL + payerInfo.getCertifId() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCvn2() && !payerInfo.getCvn2().isEmpty()) {
                payerInfoBuff.append("cvn2" + UpqcParamConstant.EQUAL + payerInfo.getCvn2() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getExpired() && !payerInfo.getExpired().isEmpty()) {
                payerInfoBuff.append("expired" + UpqcParamConstant.EQUAL + payerInfo.getExpired() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getMobile() && !payerInfo.getMobile().isEmpty()) {
                payerInfoBuff.append("mobile" + UpqcParamConstant.EQUAL + payerInfo.getMobile() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payerInfo.getCardAttr() && !payerInfo.getCardAttr().isEmpty()) {
                payerInfoBuff.append("cardAttr" + UpqcParamConstant.EQUAL + payerInfo.getCardAttr() + UpqcParamConstant.AMPERSAND);
            }
            payerInfoBuff.setLength(payerInfoBuff.length() - 1);
            payerInfoBuff.append(UpqcParamConstant.RIGHT_BRACE);
            xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.payInfoStr, encData(payerInfoBuff.toString()));
        }
    }

    private void packagePayeeInfo(PayeeInfoBean payeeInfoBean) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (payeeInfoBean != null && isNotEmpty(payeeInfoBean)) {
            StringBuffer payeeInfo = new StringBuffer(UpqcParamConstant.LEFT_BRACE);
            //StringBuffer payeeInfo = new StringBuffer();
            if (null != payeeInfoBean.getName() && !payeeInfoBean.getName().isEmpty()) {
                payeeInfo.append("name" + UpqcParamConstant.EQUAL + payeeInfoBean.getName() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getId() && !payeeInfoBean.getId().isEmpty()) {
                payeeInfo.append("id" + UpqcParamConstant.EQUAL + payeeInfoBean.getId() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getMerCatCode() && !payeeInfoBean.getMerCatCode().isEmpty()) {
                payeeInfo.append("merCatCode" + UpqcParamConstant.EQUAL + payeeInfoBean.getMerCatCode() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getTermId() && !payeeInfoBean.getTermId().isEmpty()) {
                payeeInfo.append("termId" + UpqcParamConstant.EQUAL + payeeInfoBean.getTermId() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getAccNo() && !payeeInfoBean.getAccNo().isEmpty()) {
                payeeInfo.append("accNo" + UpqcParamConstant.EQUAL + payeeInfoBean.getAccNo() + UpqcParamConstant.AMPERSAND);
            }
            payeeInfo.setLength(payeeInfo.length() - 1);
            payeeInfo.append(UpqcParamConstant.RIGHT_BRACE);
            xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.payeeInfoStr, encData(payeeInfo.toString()));
        }
    }
}
